package com.gxufl.service;

import java.util.ArrayList;
import java.util.List;

import com.gxufl.Application;
import com.gxufl.dao.NoticeDao;
import com.gxufl.dao.RoomDao;
import com.gxufl.dao.SeatDao;
import com.gxufl.entity.Notice;
import com.gxufl.entity.Room;
import com.gxufl.entity.Seat;

public class RoomService {
	RoomDao dao=new RoomDao();
	SeatDao seatDao = new SeatDao();
	
	public List<Room> searchRoom(String name){
		List<Room> list= dao.findList(name);
		return list;
	}
	
	public void saveRoom(Room room) {
		//验证影厅名称唯一性
		Room _room=dao.findOne(room.name);
		boolean isCheckOk=false;
		if(_room==null){
			isCheckOk=true;
		}
		if(!isCheckOk){
			System.out.println("影厅重名！");
			Application.fn显示主菜单();
			return;
		}
		room = dao.save(room);
		// TODO: 根据影厅座位数，自动批量新增座位数据座位编码为1-1，1-2，...，2-1，2-2
		
		List<Seat> list = new ArrayList<>();
		for(int i = 0, ii = room.num; i < ii; i++) {
			
			//TODO: 构造座位实体
			int row = (i / 10) + 1;
			int col = (i % 10) + 1;
			String code = row+"-"+col;
			
			Seat seat = new Seat(room.id, room.name, code, false);
			list.add(seat);

		}
		
		seatDao.save(list);
	}

	public void deleteRoom(String name) {
		dao.delete2(name);
	}
	
	public Room searchOneRoom(String roomname){
		List<Room> list=this.searchRoom(roomname);
		if(list.size()==1){
			return list.get(0);
		}else if(list.size()==0){
			System.out.println("没有找到"+roomname);
		}else{
			System.out.println("错误：找到"+list.size()+"个影厅");
		}
		return null;
	}
}
